#pragma once
#include <mutex>



/**
 * 单列获取工具类
 * @tparam T
 */
template<typename T>
class SingletonTool {

private:
    static T* m_instance;
    static std::mutex m_mutex;

public:

    /**
     * 获取单例
     * @return
     */
    static T* instance(){

        if(SingletonTool::m_instance != nullptr) return SingletonTool::m_instance;

        std::unique_lock<std::mutex> lock (SingletonTool::m_mutex);

        if(SingletonTool::m_instance == nullptr) SingletonTool::m_instance = new T();


        return  SingletonTool::m_instance;

    }




private:
    SingletonTool(){}
    SingletonTool(const SingletonTool<T> &);
    SingletonTool<T>& operator = (const SingletonTool<T>&);
    ~SingletonTool(){}


};


template<typename T>
T* SingletonTool<T>::m_instance = nullptr;


template<typename T>
std::mutex SingletonTool<T>::m_mutex;